--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--开服英雄翅膀


function OpenServerHeroSwingData(sysarg, packet)
	local opType = DataPack.readByte(packet)
	if opType == 0 then
		SendOpenServerHeroSwing(sysarg)
	elseif opType == 1 then
		local HeroSwingCfg = OpenServerCfg.HeroSwing
		local sVar = Actor.getStaticVar(sysarg)
		if not sVar.oSHeroSwingActiveState then
			sVar.oSHeroSwingActiveState = 0
		end
		if not sVar.oSHeroSwingActiveDay then
			sVar.oSHeroSwingActiveDay = 0
		end
		if not sVar.oSHeroSwingAwardState then
			sVar.oSHeroSwingAwardState = 0
		end
		local days = Actor.getLoginDays(sysarg)
		local activeDay = 0
		if sVar.oSHeroSwingActiveState == 1 then
			activeDay = days - sVar.oSHeroSwingActiveDay + 1
		end
		if sVar.oSHeroSwingActiveState == 1 and sVar.oSHeroSwingAwardState == 0 and activeDay >= HeroSwingCfg.needLoginDays then
			sVar.oSHeroSwingAwardState = 1
		end
		if sVar.oSHeroSwingAwardState == 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerHeroSwing001, ttFlyTip)
			return
		elseif sVar.oSHeroSwingAwardState == 2 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerHeroSwing002, ttFlyTip)
			return
		end
		local rewards = HeroSwingCfg.awards
		if IsBagGridEnough(sysarg, rewards) then
			sVar.oSHeroSwingAwardState = 2
			GiveCommonAward(sysarg, rewards, LogIds.OpenServerHeroSwingLogId, Lang.LogTips.OpenServerHeroSwing)
		else
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		end	
	end
end

function SendOpenServerHeroSwing(sysarg)
	local HeroSwingCfg = OpenServerCfg.HeroSwing
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.oSHeroSwingActiveState then
		sVar.oSHeroSwingActiveState = 0
	end
	if not sVar.oSHeroSwingActiveDay then
		sVar.oSHeroSwingActiveDay = 0
	end
	if not sVar.oSHeroSwingAwardState then
		sVar.oSHeroSwingAwardState = 0
	end
	local days = Actor.getLoginDays(sysarg)
	local activeDay = 0
	if sVar.oSHeroSwingActiveState == 1 then
		activeDay = days - sVar.oSHeroSwingActiveDay + 1
	end
	if sVar.oSHeroSwingActiveState == 1 and sVar.oSHeroSwingAwardState == 0 and activeDay >= HeroSwingCfg.needLoginDays then
		sVar.oSHeroSwingAwardState = 1
	end
	local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sOpenServerHeroSwingData)
	if npack == nil then 
		return 
	end
	--print("sVar.oSHeroSwingActiveState or 0 ="..sVar.oSHeroSwingActiveState or 0)
	DataPack.writeByte(npack, sVar.oSHeroSwingActiveState or 0)
	--print("activeDay ="..activeDay)
	DataPack.writeByte(npack, activeDay)
	--print("sVar.oSHeroSwingAwardState or 0 ="..sVar.oSHeroSwingAwardState or 0)
	DataPack.writeByte(npack, sVar.oSHeroSwingAwardState or 0)
	DataPack.flush(npack) 

end

function OpenServerHeroSwingActive(sysarg, heroSwingIdx)
	local HeroSwingCfg = OpenServerCfg.HeroSwing
	local days = System.getDaysSinceOpenServer()
	if days > HeroSwingCfg.endDay or days < HeroSwingCfg.startDay then
		return
	end
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.oSHeroSwingActiveState then
		sVar.oSHeroSwingActiveState = 0
	end
	if heroSwingIdx == HeroSwingCfg.needHeroSwingIdx then
		sVar.oSHeroSwingActiveState = 1
		sVar.oSHeroSwingActiveDay = Actor.getLoginDays(sysarg)

		SendOpenServerHeroSwing(sysarg)
	end
end

function OnOpenServerHeroSwingLogin(sysarg)
	if System.getDaysSinceCombineServer() <= 0 then
		return 
	end
	local sVar = Actor.getStaticVar(sysarg)
	local days = Actor.getLoginDays(sysarg)
	if not sVar.oSHeroSwingActiveState then
		sVar.oSHeroSwingActiveState = 0
	end
	if not sVar.oSHeroSwingAwardState then
		sVar.oSHeroSwingAwardState = 0
	end
	if not sVar.oSHeroSwingActiveDay then
		sVar.oSHeroSwingActiveDay = 0
	end
	local activeDay = 0
	local HeroSwingCfg = OpenServerCfg.HeroSwing
	if sVar.oSHeroSwingActiveState == 1 then
		activeDay = days - sVar.oSHeroSwingActiveDay + 1
	end
	if sVar.oSHeroSwingActiveState == 1 and sVar.oSHeroSwingAwardState == 0 and activeDay >= HeroSwingCfg.needLoginDays then
		sVar.oSHeroSwingAwardState = 1
	end
	if sVar.oSHeroSwingAwardState == 1 then
		sVar.oSHeroSwingAwardState = 2
		local rewards = HeroSwingCfg.awards
		Awards.GiveToMailById(Actor.getActorId(sysarg), rewards, Lang.ScriptTips.OpenServerHeroSwing004, Lang.ScriptTips.OpenServerHeroSwing005,  LogIds.OpenServerHeroSwingLogId, Lang.LogTips.OpenServerHeroSwing)
	end
end